package com.guomei.controller.curse;

import com.guomei.bean.curse.Course;
import com.guomei.bean.curse.CourseClassAssociation;
import com.guomei.mapper.curse.CourseMapper;
import com.guomei.service.curse.CourseClassAssociationService;
import com.guomei.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 课程班关联课程控制器
 */
@RestController
@RequestMapping("/api1/courseClassAssociation")
@Slf4j
public class CourseClassAssociationController {
    @Autowired
    private CourseClassAssociationService courseClassAssociationService;

    @Resource
    private CourseMapper courseMapper;

    /**
     * 获取课程班关联信息
     * @return 关联列表
     */
    @PostMapping("/select")
    public Result<?> getAllAssociations(@RequestBody CourseClassAssociation courseClassAssociation) {
        log.info("获取课程班关联信息");
        try {
            if (courseClassAssociation == null || courseClassAssociation.getPageNum() == null || courseClassAssociation.getPageSize() == null) {
                return Result.fail("分页字段信息缺失，查询失败");
            }
            // 验证分页参数是否有效
            if (courseClassAssociation.getPageNum() <= 0 || courseClassAssociation.getPageSize() <= 0) {
                return Result.fail("分页参数无效");
            }
            return courseClassAssociationService.getByCondition(courseClassAssociation);
        } catch (Exception e) {
            log.info("获取课程班关联信息失败", e);
            return Result.fail("获取课程班关联信息失败");
        }
    }

    /**
     * 根据ID获取关联详情
     * @param id 关联ID
     * @return 关联详情
     */
    @GetMapping("/detail/{id}")
    public Result<?> getAssociationDetail(@PathVariable Long id) {
        log.info("获取课程班关联详情，ID：{}", id);
        try {
            if (id == null) {
                return Result.fail("关联ID不能为空");
            }
            CourseClassAssociation association = courseClassAssociationService.getById(id);
            if (association == null) {
                return Result.fail("关联记录不存在");
            }
            return Result.success(association);
        } catch (Exception e) {
            log.info("获取课程班关联详情失败", e);
            return Result.fail("获取课程班关联详情失败");
        }
    }

    /**
     * 新增课程班关联
     * @param courseClassAssociation 关联信息
     * @return 新增结果
     */
    @PostMapping("/add")
    public Result<?> addAssociation(@RequestBody CourseClassAssociation courseClassAssociation) {
        log.info("新增课程班关联入参：{}", courseClassAssociation);
        try {
            return courseClassAssociationService.save(courseClassAssociation);
        } catch (Exception e) {
            log.info("新增课程班关联失败", e);
            return Result.error("新增失败");
        }
    }

    /**
     * 更新课程班关联
     * @param courseClassAssociation 关联信息
     * @return 更新结果
     */
    @PostMapping("/update")
    public Result<?> updateAssociation(@RequestBody CourseClassAssociation courseClassAssociation) {
        log.info("更新课程班关联入参：{}", courseClassAssociation);
        try {
            if (courseClassAssociation.getMainCourseId() == null) {
                return Result.fail("主课程id不能为空");
            }
            if (courseClassAssociation.getDeputyCourseId() == null || courseClassAssociation.getDeputyCourseIds().isEmpty()){
                return Result.fail("下属课程id不能为空");
            }
            boolean success = courseClassAssociationService.update(courseClassAssociation);
            return success ? Result.success("更新成功") : Result.fail("更新失败");
        } catch (Exception e) {
            log.info("更新课程班关联失败", e);
            return Result.error("更新失败");
        }
    }

    /**
     * 删除课程班关联
     * @param id 关联ID
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    public Result<?> deleteAssociation(@PathVariable Long id) {
        log.info("删除课程班关联，ID：{}", id);
        try {
            if (id == null) {
                return Result.fail("关联ID不能为空");
            }
            boolean success = courseClassAssociationService.removeById(id);
            return success ? Result.success("删除成功") : Result.fail("删除失败");
        } catch (Exception e) {
            log.info("删除课程班关联失败", e);
            return Result.fail("删除课程班关联失败");
        }
    }

    /**
     * 根据主课程ID获取关联列表
     * @param mainCourseId 主课程ID
     * @return 关联列表
     */
    @GetMapping("/mainCourse/{mainCourseId}")
    public Result<?> getAssociationsByMainCourseId(@PathVariable Long mainCourseId) {
        log.info("根据主课程ID获取关联列表，主课程ID：{}", mainCourseId);
        try {
            if (mainCourseId == null) {
                return Result.fail("主课程ID不能为空");
            }
            List<CourseClassAssociation> associations = courseClassAssociationService.getByMainCourseId(mainCourseId);
            return Result.success(associations);
        } catch (Exception e) {
            log.info("根据主课程ID获取关联列表失败", e);
            return Result.fail("获取关联列表失败");
        }
    }

    /**
     * 根据下属课程ID获取关联列表
     * @param deputyCourseId 下属课程ID
     * @return 关联列表
     */
    @GetMapping("/deputyCourse/{deputyCourseId}")
    public Result<?> getAssociationsByDeputyCourseId(@PathVariable Long deputyCourseId) {
        log.info("根据下属课程ID获取关联列表，下属课程ID：{}", deputyCourseId);
        try {
            if (deputyCourseId == null) {
                return Result.fail("下属课程ID不能为空");
            }
            List<CourseClassAssociation> associations = courseClassAssociationService.getByDeputyCourseId(deputyCourseId);
            return Result.success(associations);
        } catch (Exception e) {
            log.info("根据下属课程ID获取关联列表失败", e);
            return Result.fail("获取关联列表失败");
        }
    }

    /**
     * 根据主课程ID删除所有关联
     * @param mainCourseId 主课程ID
     * @return 删除结果
     */
    @DeleteMapping("/mainCourse/{mainCourseId}")
    public Result<?> deleteByMainCourseId(@PathVariable Long mainCourseId) {
        log.info("根据主课程ID删除所有关联，主课程ID：{}", mainCourseId);
        try {
            if (mainCourseId == null) {
                return Result.fail("主课程ID不能为空");
            }
            boolean success = courseClassAssociationService.removeByMainCourseId(mainCourseId);
            return success ? Result.success("删除成功") : Result.fail("删除失败");
        } catch (Exception e) {
            log.info("根据主课程ID删除所有关联失败", e);
            return Result.fail("删除失败");
        }
    }

    /**
     * 根据下属课程ID删除所有关联
     * @param deputyCourseId 下属课程ID
     * @return 删除结果
     */
    @DeleteMapping("/deputyCourse/{deputyCourseId}")
    public Result<?> deleteByDeputyCourseId(@PathVariable Long deputyCourseId) {
        log.info("根据下属课程ID删除所有关联，下属课程ID：{}", deputyCourseId);
        try {
            if (deputyCourseId == null) {
                return Result.fail("下属课程ID不能为空");
            }
            boolean success = courseClassAssociationService.removeByDeputyCourseId(deputyCourseId);
            return success ? Result.success("删除成功") : Result.fail("删除失败");
        } catch (Exception e) {
            log.info("根据下属课程ID删除所有关联失败", e);
            return Result.fail("删除失败");
        }
    }

    /**
     * 检查关联是否存在
     * @param mainCourseId 主课程ID
     * @param deputyCourseId 下属课程ID
     * @return 检查结果
     */
    @GetMapping("/check/{mainCourseId}/{deputyCourseId}")
    public Result<?> checkAssociation(@PathVariable Long mainCourseId, @PathVariable Long deputyCourseId) {
        log.info("检查关联是否存在，主课程ID：{}，下属课程ID：{}", mainCourseId, deputyCourseId);
        try {
            if (mainCourseId == null) {
                return Result.fail("主课程ID不能为空");
            }
            if (deputyCourseId == null) {
                return Result.fail("下属课程ID不能为空");
            }
            CourseClassAssociation association = courseClassAssociationService.getByMainAndDeputyCourseId(mainCourseId, deputyCourseId);
            return Result.success(association != null);
        } catch (Exception e) {
            log.info("检查关联是否存在失败", e);
            return Result.fail("检查失败");
        }
    }
}
